package tree;
import java.util.*;

public class TreeNode implements Node {
	
	ArrayList<Node> children;
	int index;
	int winner = -2;
	int turn;
	
	public TreeNode(ArrayList<Node> children, int turn, int index) {
		this.children = children;
		this.turn = turn;
		this.index = index;
	}
	
	public int winner() {
		if(winner != -2)
			return winner;
		
		for(Node node : children) {
			int childWinner = node.winner();
			if(childWinner == 0)
				winner = 0;
			else if(childWinner == -turn) {
				winner = childWinner;
				break;
			}
			else if(winner != 0 && childWinner == turn)
				winner = turn;
		}
		
		return winner; 
	}

	public int getIndex() {
		return index;
	}
	
	public Iterator<Node> iterator() {
		return children.iterator();
	}
}
